﻿app.controller('ChurchMemberJobtitleAssignmentController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', '$http', 'GlobalFactory', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice, $http, GlobalFactory) {

    $scope.currentPage = 1;
    $scope.searchkey = "";
    $scope.sortmode = "DESC";
    $scope.sortby = "DEFAULT";
    $scope.showgridloader = true;
    $scope.viewmode = 'TABLE';
    $scope.perpage = 10;
    $scope.jobtitle = {};
    var currentTime = new Date();

    BindGrid();
    BindChurchMember();
    BindBoardName();
    BindStatus();
    BindYear();

    $scope.sort = function () {
        if ($scope.sortmode == 'ASC') {
            $scope.sortmode = 'DESC';
        }
        else {
            $scope.sortmode = 'ASC';
        }
        BindGrid();
    }

    $scope.$watch('jobtitle.ChurchBoardId', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            BindJobtitle(newVal);
        }
    });

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Jobtitle";
                $scope.jobtitle = {};
                break;
            case 'edit':
                $scope.title = "Edit Jobtitle";
                url = "GetChurchMemberJobtitle";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.jobtitle = data;
                    var value = { name: $scope.jobtitle.Members.FirstName, phone: "", picture: "", MemberId: $scope.jobtitle.MemberId, Email: $scope.jobtitle.Email };
                    $scope.churchmember = value;
                });
                break;
        }
        $("#Jobtitle").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        $scope.showgridloader = true;
        url = "GetChurchMemberJobtitles?PageNo=" + $scope.currentPage + "&SearchKey=" + $scope.searchkey + "&Sort=" + $scope.sortby + "&SortMode=" + $scope.sortmode + "&PerPage=" + $scope.perpage;
        ApplicationService.Getdata(url).success(function (data) {
            $scope.showgridloader = false;
            $scope.list = data;
            if (data.length > 0) {
                $scope.entryLimit = 10;
                $scope.filteredItems = data[0].TotalCounts;
                $scope.totalItems = data[0].TotalCounts;
            }
        });
    }

    function BindChurchMember() {
        GlobalFactory.GetChurchMemberbyChurchId().then(function (responce) {
            $scope.churchmembers = [];
            for (var i = 0; i <= responce.data.length-1; i++) {
                var value = { name: responce.data[i].FirstName, phone: responce.data[i].Phone, picture: responce.data[i].Photo, MemberId: responce.data[i].MemberId, Email: responce.data[i].Email };
                $scope.churchmembers.push(value);
            }
        });
    }

    $scope.showcheckbox = false;
    $scope.$watch('churchmember', function (newVal) {
        if (angular.isUndefined(newVal) == false) {
            if(newVal.Email!='')
            {
                $scope.jobtitle.Email = newVal.Email;
                $scope.showcheckbox = true;
            }
            else {
                $scope.jobtitle.CreateLogin = "";
                $scope.jobtitle.Email = '';
                $scope.showcheckbox = false;
            }
        }
    });
    
    function BindBoardName()
    {
        url = "../../Admin/Master/GetChurchBoards";
        ApplicationService.Getdata(url).success(function (data) {
            $scope.ChurchBoards = data;
            
        });
    }

    function BindJobtitle(BoardId)
    {
        url = "../../Admin/Master/GetChurchBoardAssignedJobtitle";
        ApplicationService.Getdata(url, BoardId).success(function (data) {
            $scope.ChurchBoardJobtitle = data;
            console.log($scope.ChurchBoardJobtitle);
        });
    }

    function BindStatus()
    {
        GlobalService.GetChurchWorkerStatus().success(function (data) { $scope.ChurchWorkerStatuss = data; });
    }

    function BindYear()
    {
        $scope.years = [];
        for (var i = currentTime.getFullYear() - 5; i <= currentTime.getFullYear() ; i++) {
            var year = { yearid: i, year: i };
            $scope.years.push(year);
        }
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
        BindGrid();
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "DeleteChurchMemberJobtitle";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    $scope.save = function (modelstate, id) {
        $scope.jobtitle.MemberId = $scope.churchmember.MemberId;
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "InsertChurchMemberJobtitle";
                break;
            case 'edit':
                url = "UpdateChurchMemberJobtitle";
                break;
        }
        $scope.sortmode = "DESC";
        $scope.sortby = "DEFAULT";
        ApplicationService.Save($scope.jobtitle, url).success(function (result) {
            alertboxservice.ShowMsg(result, "Jobtitle ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

    $scope.ChangeView = function () {
        if ($scope.viewmode == 'TABLE') {
            $scope.viewmode = 'CARD';
        }
        else {
            $scope.viewmode = 'TABLE';
        }
    }


}]);

